package cn.edu.ujn.ch13.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.ujn.ch13.model.User;
import cn.edu.ujn.ch13.model.UserVO;

@Controller
public class UserController {

	@RequestMapping("/delete")
	public String toDelete(Model model) {
		ArrayList<User> list = new ArrayList<User>();
		User user1 = new User();
		user1.setId(101);
		user1.setUsername("shaohui");
		list.add(user1);

		User user2 = new User();
		user2.setId(102);
		user2.setUsername("shaohui");
		list.add(user2);

		User user3 = new User();
		user3.setId(103);
		user3.setUsername("shaohui");
		list.add(user3);

		for (int i = 1; i < 10; i++) {
			User user = new User();
			user.setId(100 + i);
			user.setUsername("shaohui" + i);
			list.add(user);
		}
		model.addAttribute("userList", list);
		return "deleteUser";
	}

	@RequestMapping("/deleteUsers")
	public String deleteUsers(Integer[] ids) {
		if (ids != null) {
			for (Integer id : ids) {
				System.out.println(id + "is deleted!");
			}
		} else {
			System.out.println(ids + "are null");
		}
		return "success";
	}
	
	@RequestMapping("/edit")
	public String toEdit() {
		
		return "editUser";
	}
	@RequestMapping("/editUsers")
	public String editUsers(UserVO userlist) {
		List<User> users = userlist.getUsers();
		for(User user:users) {
			if (user.getId()!=0) {
				System.out.println(user+"成功修改！");
			}
		}
		return "success";
	}
}
